﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcApplication5.Models;
using System.Web.SessionState;

namespace MvcApplication5.Common
{
    public class AppContext
    {
        private const string _contextItemName = "AppContext";

        public AppContext(HttpContext httpContext) {
            httpContext.Items[_contextItemName] = this;
        }

        public static AppContext Current
        {
            get {
                if (HttpContext.Current == null) {
                    return null;
                }

                return HttpContext.Current.Items[_contextItemName] as AppContext;
            }        
        }

        public HttpSessionState Session
        {
            get { return Current.HttpContext.Session; }
        }

        public HttpContext HttpContext
        {
            get { return System.Web.HttpContext.Current; }
        }

        public HttpResponse HttpResponse
        {
            get { return System.Web.HttpContext.Current.Response; }
        }

        public HttpRequest Request
        {
            get { return System.Web.HttpContext.Current.Request; }        
        }

        public UserModelExt LoginUser
        {
            get
            {
                if (this.Session != null && this.Session["LoginUser"] != null && this.Session["LoginUser"] != "")
                {
                    return (UserModelExt)this.Session["LoginUser"];
                }
                else
                {
                    return new UserModelExt();
                }
            }
        }
    }
}